package com.china08.yxyapi.api.manage.tec.teaching.zujuan.previewpaper;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.service.manage.tec.teaching.zujuan.ZujuanQuestionService;

import io.swagger.annotations.ApiOperation;

/**
 * 下载试卷
 * 
 * 
 */
@RestController
@CrossOrigin
public class ZujuanPaperGenerateController extends YxyApiBaseController {

    private static final Logger LOGGER = LoggerFactory.getLogger(ZujuanPaperGenerateController.class);

    @Value("${zujuan.storage.upload.paper}")
    private String downloadDir;

    @Autowired
    private ZujuanQuestionService zujuanQuestionService;

    @ApiOperation(value = "生成试卷")
    @RequestMapping(value = "/api_yw/manage/tec/teaching/zujuan/paper/generate", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public DownLoadRespModel precess(HttpServletRequest request, HttpServletResponse response,
            @RequestBody DownLoadReqModel reqModel) throws Exception {
        String paperName = zujuanQuestionService.DownLoadPaper(reqModel, downloadDir);
        DownLoadRespModel respModel = new DownLoadRespModel();
        int lastIndexOf = paperName.lastIndexOf(".");
        respModel.paperName = StringUtils.substring(paperName, 0, lastIndexOf);
        // respModel.paperName = StringUtils.substringBefore(paperName, ".");
        respModel.setMessage("下载试卷成功");
        return respModel;
    }

    public static class DownLoadReqModel extends YxyApiReqModel {
        // 试卷答案分布（老师使用teacher,学生使用normal）
        public String paperType;
        // 试卷的纸张大小
        public String paperSize;
        // 试题请求模型
        public List<DownLoadQuesModel> queslist;
        // 试卷的基本配置
        public List<String> checkList;
        // 主标题
        public String title1;
        // 副标题
        public String title2;
        // 试卷信息栏
        public String title3;
        // 考生输入栏
        public String title4;
        // 第1卷说明
        public String title5;
        // 第2卷说明
        public String title6;
        // 单选题注释及分数
        public String title7;
        // 多选题注释及分数
        public String title8;
        // 填空题注释及分数
        public String title9;
        // 解答题注释及分数
        public String title10;
    }

    /**
     * 题的响应
     */
    public static class DownLoadQuesModel {
        // 试题id
        public String id;
        // 题的序号
        public Integer order;
        // 试题的分数
        public Integer point;
        // 题型
        public String quesType;
        // 题干
        public String quesBody;
        // 题的答案和解析
        public String analysis;
    }

    protected static class DownLoadRespModel extends YxyApiRespModel {
        // 试卷名称
        public String paperName;
    }

}
